package com.opoc.controller;

import com.opoc.pojo.Log;
import com.opoc.pojo.Result;
import com.opoc.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
public class SystemLogController {

  @Autowired
  SystemLogService systemLogService;

  @GetMapping("admin/log")
  public Result getLogs(@RequestParam(value = "operation_kind", required=false) String operation_kind,
                        @RequestParam(value = "start_time", required = false) String start_time,
                        @RequestParam(value = "end_time", required = false) String end_time) throws ParseException {
    List<Log> logs = systemLogService.getLogs(operation_kind, start_time, end_time);
    return logs.isEmpty() ? Result.error("无数据") : Result.success(logs);
  }

  @DeleteMapping("admin/log")
  @Transactional(rollbackFor = Exception.class)
  public Result deleteLogs(@RequestParam("log_ids") List<Integer> log_ids){
    systemLogService.deleteLogs(log_ids);
    return Result.success();
  }

}
